# coding=utf-8
import requests


class HtmlCrawler(object):
    def __init__(self):
        self.login_url = 'https://www.shanbay.com/accounts/login/'
        self.login_cookies = None
        self.crawler = None

    def login_html(self):
        response = requests.get(self.login_url)
        login_cookie = response.cookies
        self.login_cookies = dict(login_cookie)
        if 'csrftoken' in self.login_cookies.keys():
            '''没有登录的时候去登录'''
            login_data = {
                'csrfmiddlewaretoken': login_cookie['csrftoken'],
                'username': '',
                'password': '',
            }
            response = requests.post(self.login_url, cookies=dict(login_cookie), data=login_data, allow_redirects=False)
            self.login_cookies = dict(response.cookies)

    def download_html(self, wordbook_url, params=None):
        # if self.login_cookies is None:
            # self.login_html()

        response = requests.get(wordbook_url, params=params)
        print(response.url)
        return response.text

    @classmethod
    def get_word_main(cls, word):
        url = 'https://www.shanbay.com/api/v1/bdc/search/?version=2&word='
        response = requests.get(url + word)
        return response.json()
